using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Media;
using Nop.Plugin.Widgets.Carousel.Data;
using Nop.Plugin.Widgets.Carousel.Services;
using Nop.Web.Framework.Menu;
using System.Linq;

namespace Nop.Plugin.Widgets.Carousel
{
    /// <summary>
    /// PLugin
    /// </summary>
    public class CarouselPlugin : BasePlugin, IWidgetPlugin, IAdminMenuPlugin
    {
        private readonly IPictureService _pictureService;
        private readonly ISettingService _settingService;
        private readonly IWebHelper _webHelper;
        private readonly CarouselObjectContext _objectContext;
        private readonly ICarouselService _carouselService;
        private readonly ILocalizationService _localizationService;
        private readonly IWorkContext _workContext;

        public CarouselPlugin(IPictureService pictureService, 
            ISettingService settingService, IWebHelper webHelper,
            CarouselObjectContext objectContext, ICarouselService carouselService,
            ILocalizationService localizationService,IWorkContext workContext)
        {
            this._pictureService = pictureService;
            this._settingService = settingService;
            this._webHelper = webHelper;
            this._objectContext = objectContext;
            this._carouselService = carouselService;
            this._localizationService = localizationService;
            this._workContext = workContext;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return _carouselService.GetCarouselsWidgetZones();
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "WidgetsCarousel";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.Carousel.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "WidgetsCarousel";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.Carousel.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }
        
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //data
            _objectContext.Install();
            //configure
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Carousel.configure.title", "Carousel ");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Carousel.configure.message", "<h2>Create a Product Carousel</h2><ul><li>Add products from products list</li><li>Use Nop _productBox or a custom _productBox page</li><li>Show / Hide navigation</li><li>Set size</li><li>Multi carousel on same widget zone</li></ul><br /><h3>Set a custom _productBox</h3><ul><li>Goto Presentation -> Now.Web -> Plugins -> Widgets.Carousel -> Veiws </li><li>Open and edit in html editor _ProductBox.cshtml </li><li>Add custom style to Content -> responsiveCarousel.css</li><li>Create a new carousel</li><li>In carousel info check -> Custom ProductBox </li></ul>");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Carousel.configure.link", "Create Carousel");

            //list
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Manage", "Manage Carousels");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.SearchCarouselName", "Carousel name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.SearchCarouselName.Hint", "Search by carousel name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name.Required", "Please provide a name.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name.hint", "Carousel name.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.WidgetZone", "Widget Zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.WidgetZone.hint", "Widget Zone");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Published", "Published");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Published.Hint", "Published");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.DisplayOrder", "Display order");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.DisplayOrder.Hint", "Display order");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.isHiddenNav", "Hide Navigation Menu");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.isHiddenNav.Hint", "Hide Navigation Menu");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.NumberOfItems", "Number of Items");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.NumberOfItems.Hint", "Number of Items");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMinWidth", "Item Min-Width");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMinWidth.Hint", "Item Min-Width");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMargin", "Item Margin");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMargin.Hint", "Item Margin");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.AutoRotate", "AutoRotate rate (ms)");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.AutoRotate.Hint", "AutoRotate rate");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.IsAutoRotate", "Auto Rotate");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.IsAutoRotate.Hint", "Auto Rotate");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.CustomProductBox", "Use custom product box");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.CustomProductBox.Hint", "Use custom product box");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Info", "Carousel Info");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Pictures", "Carousel Images");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.SaveBeforeEdit", "You need to save the carousel.");
            
            //entity list
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ProductName", "Product Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Fields.PictureThumbnailUrl", "Picture");
            
            //create
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.AddNew", "Add A New Carousel");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.BackToList", "back to Carousel list");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.AddNewPicture", "Add new picture");
            //edit
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.EditCarouselDetails", " Edit Carousel Details");
            
            //other
            this.AddOrUpdatePluginLocaleResource("ActivityLog.EditCarousel", "Edit Carousel");
            this.AddOrUpdatePluginLocaleResource("ActivityLog.AddNewCarousel", "Carousel Added");
            this.AddOrUpdatePluginLocaleResource("ActivityLog.DeleteCarousel", "Carousel Deleted");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Updated", "The Carousel has been updated successfully.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Added", "The new Carousel has been added successfully.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.Deleted", "The Carousel has been deleted successfully.");

            //menu
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Carousel.AdminMenuText", "Carousel");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //data
            _objectContext.Uninstall();

            //configure
            this.DeletePluginLocaleResource("plugins.widgets.Carousel.configure.title");
            this.DeletePluginLocaleResource("plugins.widgets.Carousel.configure.message");
            this.DeletePluginLocaleResource("plugins.widgets.Carousel.configure.link");

            //list
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Manage");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.SearchCarouselName");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.SearchCarouselName.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Name.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.WidgetZone");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.WidgetZone.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Published");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.Published.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.DisplayOrder");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.DisplayOrder.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.isHiddenNav");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.isHiddenNav.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.NumberOfItems");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.NumberOfItems.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMinWidth");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMinWidth.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMargin");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ItemMargin.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.AutoRotate");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.AutoRotate.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.IsAutoRotate");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.IsAutoRotate.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.CustomProductBox");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.CustomProductBox.Hint");
            //entity list
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.ProductName");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Fields.PictureThumbnailUrl");
            
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Info");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Pictures");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.PreviewMessage");

            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.SaveBeforeEdit");

            //create
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.AddNew");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.BackToList");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.AddNewPicture");
            
            //edit
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.EditCarouselDetails");
            
            //other
            this.DeletePluginLocaleResource("ActivityLog.EditCarousel");
            this.DeletePluginLocaleResource("ActivityLog.AddNewCarousel");
            this.DeletePluginLocaleResource("ActivityLog.DeleteCarousel");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Updated");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Added");
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.Deleted");
            //admin menu
            this.DeletePluginLocaleResource("Plugins.Widgets.Carousel.AdminMenuText");
            base.Uninstall();
        }

        public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Hz",
                Title = _localizationService.GetResource("Plugins.Widgets.Carousel.AdminMenuText"),
                ControllerName = "WidgetsCarousel",
                ActionName = "List",
                Visible = true,
                RouteValues = new RouteValueDictionary() { { "area", null } },
            };

            var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
            if (pluginNode != null)
                pluginNode.ChildNodes.Add(menuItem);
            else
                rootNode.ChildNodes.Add(menuItem);
        }
    }
}
